import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:freedom/const/location_enum.dart';
import 'package:freedom/const/mahjong_system_const.dart';

class MahjongCommon {
  static Widget buildHuLocation(bool showWidget, Location? userLocation) {
    if (!showWidget) {
      return SizedBox();
    }
    if (userLocation == null) {
      return Container(
        alignment: Alignment.center,
        width: MahjongSystemConst.userHeaderImageWidth,
        height: 18.h,
        decoration: BoxDecoration(
            color: Colors.orange, borderRadius: BorderRadius.circular(5.h)),
        child: Text(
          "自摸",
          textAlign: TextAlign.center,
          style: TextStyle(fontSize: 10.sp, color: Colors.white),
        ),
      );
    }
    IconData iconData;
    switch (userLocation) {
      case Location.BOTTOM:
        iconData = Icons.arrow_downward;
        break;
      case Location.LEFT:
        iconData = Icons.arrow_back;
        break;
      case Location.RIGHT:
        iconData = Icons.arrow_forward;
        break;
      case Location.TOP:
        iconData = Icons.arrow_upward;
        break;
      default:
        iconData = Icons.arrow_upward;
    }

    return Container(
      width: MahjongSystemConst.userHeaderImageWidth,
      alignment: Alignment.center,
      height: 18.h,
      decoration: BoxDecoration(
          color: Colors.orange, borderRadius: BorderRadius.circular(5.h)),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Icon(
            iconData,
            color: Colors.white,
            size: 15.sp,
          ),
          Text(
            "胡牌",
            textAlign: TextAlign.center,
            style: TextStyle(fontSize: 10.sp, color: Colors.white),
          ),
        ],
      ),
    );
  }

  static Widget headerImage(String? portraitUrl,
      {double? height, double? width}) {
    return ClipRRect(
        child: SizedBox(
      height: height ?? MahjongSystemConst.userHeaderImageWidth,
      width: width ?? MahjongSystemConst.userHeaderImageWidth,
      child: portraitUrl == null
          ? Image.asset(
              "assets/images/mahjong/user/header/common_header.png",
              fit: BoxFit.cover,
            )
          : CachedNetworkImage(
              width: double.infinity,
              height: double.infinity,
              fit: BoxFit.cover,
              placeholder: (context, url) => const CircularProgressIndicator(),
              errorWidget: (context, url, error) => const Icon(Icons.error),
              imageUrl: portraitUrl,
            ),
    ));
  }

  static buildUserStateText(bool showText, String text,
      {Color color = Colors.yellow}) {
    return showText
        ? Text(
            text,
            style: TextStyle(
                fontSize: 8.sp, color: color, fontWeight: FontWeight.w800),
          )
        : SizedBox();
  }
}
